package com.wiz.web.controllor;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wiz.common.ResponseCode;
import com.wiz.common.util.DateUtil;
import com.wiz.web.service.ApiService;
import com.wiz.web.service.MemberService;
import com.wiz.web.vo.criteria.HqStationCriteria;
import com.wiz.web.vo.criteria.ListCriteria;
import com.wiz.web.vo.type.PayChannelType;

/**
 * 数据中心对外开放接口Controllor
 */
@SuppressWarnings("all")
@Controller
@RequestMapping("/api")
public class ApiControllor implements ResponseCode {
	private transient final Logger log = LoggerFactory.getLogger(getClass());

	@Resource
	private ApiService apiService;
	@Resource
	private MemberService memberService;

	@RequestMapping(value = "/sync", method = RequestMethod.POST)
	@ResponseBody
	public String syncFromClient(HttpServletRequest request,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			@RequestParam(value = "type") Integer type,
			@RequestParam(value = "updateTime") Long updateTime,
			HttpSession session, Model model){
		try {
			return apiService.issued(hqId,stationId,type,DateUtil.formatDateTime(updateTime));
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		return RESPONSE_CODE_SERVER_ERROR;
	}
	
	@RequestMapping(value = "/soo", method = RequestMethod.POST)
	@ResponseBody
	public String getStationOnlineInfo(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			HttpSession session, Model model){
		try{
			ListCriteria<HqStationCriteria> listCriteria = ListCriteria.fromJson(params, HqStationCriteria.class);
			List<HqStationCriteria> criteriaList = listCriteria.getDataList();
			return apiService.getStationOnlineInfo(criteriaList);
		}catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		return RESPONSE_CODE_SERVER_ERROR;
	}
	
	@RequestMapping(value = "/charge", method = RequestMethod.POST)
	@ResponseBody
	public String chargeByWx(HttpServletRequest request,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "openId") String openId,
			@RequestParam(value = "channel") Integer channel,
			@RequestParam(value = "chargeAmount") Integer chargeAmount,
			@RequestParam(value = "refId") String refId,
			HttpSession session, Model model){
		try{
			if(channel == PayChannelType.WEIXIN.getId()){
				return memberService.chargeAmountByWx(hqId, openId, chargeAmount, refId);
			}else{
				return RESPONSE_CODE_PAY_CHANNEL_ERROR;
			}
		}catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		return RESPONSE_CODE_SERVER_ERROR;
	}
}
